﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Xml;

public partial class admin_Default : System.Web.UI.Page
{
    DBControl conDB = new DBControl(WebConfigurationManager.ConnectionStrings["TestBase"].ConnectionString);
    XmlDataSource catSource = new XmlDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        catSource.EnableCaching = false;
        CatTreeView.DataSource = catSource;

        FillCatTree();
    }

    protected void FillCatTree()
    {
        catIdHiddenField.Value = CatTreeView.SelectedValue;
		parentCatIdHiddenField.Value = CatParentDropDownList.SelectedValue;
        catNameHiddenField.Value = CatNameTextBox.Text;
        shortListHiddenField.Value = ShortListCheckBox.Checked.ToString();

        XmlDocument categoriesXml = conDB.getCategoriesXml(0);
        catSource.Data = categoriesXml.OuterXml;
        CatTreeView.DataBind();

        FillCatControls();
    }

    protected void FillCatControls()
    {
        if (CatTreeView.SelectedValue != "")
        {
            if (CatTreeView.SelectedValue != "0")
            {
                CategoryDetails cat = conDB.GetCategory(Convert.ToInt64(CatTreeView.SelectedValue));
                CatNameTextBox.Text = HttpUtility.HtmlDecode(cat.CategoryName);
                ShortListCheckBox.Checked = cat.ShowInShortList;

                UpdateButton.Enabled = true;
                DeleteButton.Enabled = true;
                MoveUpButton.Enabled = true;
                MoveDownButton.Enabled = true;

                CatParentDropDownList.SelectedIndex = CatParentDropDownList.Items.IndexOf(CatParentDropDownList.Items.FindByValue(cat.ParentId.ToString()));
            }
            else
            {
                CatNameTextBox.Text = "";
                ShortListCheckBox.Checked = false;

                UpdateButton.Enabled = false;
                DeleteButton.Enabled = false;
                MoveUpButton.Enabled = false;
                MoveDownButton.Enabled = false;
            }
        }
        else
        {
            CatNameTextBox.Text = "";
            ShortListCheckBox.Checked = false;

            UpdateButton.Enabled = false;
            DeleteButton.Enabled = false;
            MoveUpButton.Enabled = false;
            MoveDownButton.Enabled = false;
        }
    }

    protected void CatTreeView_SelectedNodeChanged(object sender, EventArgs e)
    {
        FillCatControls();
    }

    protected void DeleteButton_Click(object sender, EventArgs e)
    {
        if (conDB.GetChildCount(Convert.ToInt64(catIdHiddenField.Value)) == 0)
        {
            conDB.DeleteCategory(Convert.ToInt64(catIdHiddenField.Value));

            CatParentDropDownList.DataBind();
            FillCatTree();
        }
        else
        {
            DeleteCustomValidator.IsValid = false;
        }
    }

    protected void InsertButton_Click(object sender, EventArgs e)
    {
		conDB.InsertCategory(Convert.ToInt64(parentCatIdHiddenField.Value), catNameHiddenField.Value, Convert.ToBoolean(shortListHiddenField.Value));

        CatParentDropDownList.DataBind();
        FillCatTree();
    }

    protected void UpdateButton_Click(object sender, EventArgs e)
    {
        if (catIdHiddenField.Value != parentCatIdHiddenField.Value)
        {
			conDB.UpdateCategory(Convert.ToInt64(catIdHiddenField.Value), Convert.ToInt64(parentCatIdHiddenField.Value), catNameHiddenField.Value, Convert.ToBoolean(shortListHiddenField.Value));

            CatParentDropDownList.DataBind();
            FillCatTree();
        }
        else
        {
            CatParentCustomValidator.IsValid = false;
        }
    }
    
    protected void MoveUpButton_Click(object sender, EventArgs e)
    {
        conDB.MoveCategoryUp(Convert.ToInt64(catIdHiddenField.Value));

        CatParentDropDownList.DataBind();
        FillCatTree();
    }
    
    protected void MoveDownButton_Click(object sender, EventArgs e)
    {
        conDB.MoveCategoryDown(Convert.ToInt64(catIdHiddenField.Value));

        CatParentDropDownList.DataBind();
        FillCatTree();
    }
}
